<?php

namespace common\models\position;

use Yii;
use common\helpers\Url;
use common\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%position_data}}".
 *
 * @property int $id
 * @property int $position_id 推荐位ID
 * @property string $title 标题
 * @property string|null $description 简介
 * @property string|null $thumb 封面
 * @property int $type 跳转类型
 * @property string $model 模型
 * @property string|null $model_id 模型参数
 * @property int $map_id 内部路由ID
 * @property string|null $map_data 内部路由参数
 * @property string|null $url 跳转链接
 * @property int $listorder 排序
 * @property int $admin_id 添加人ID
 * @property int $status 状态：0禁用，10启用
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class PositionData extends \common\db\ActiveRecord {

    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;
    //
    const MODEL_ARTICLE = 'article';
    const MODEL_ACTIVITY = 'activity';

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%position_data}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['position_id', 'type', 'map_id', 'listorder', 'admin_id', 'status', 'created_at', 'updated_at'], 'integer'],
            [['title', 'model'], 'required'],
            [['title', 'model', 'model_id'], 'string', 'max' => 50],
            [['description', 'thumb', 'map_data', 'url'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'position_id' => '推荐位ID',
            'title' => '标题',
            'description' => '简介',
            'thumb' => '封面',
            'type' => '跳转类型',
            'model' => '模型',
            'model_id' => '模型参数',
            'map_id' => '内部路由ID',
            'map_data' => '内部路由参数',
            'url' => '跳转链接',
            'listorder' => '排序',
            'admin_id' => '添加人ID',
            'status' => '状态：0禁用，10启用',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getPosition() {
        return $this->hasOne(Position::className(), ['id' => 'position_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getSibling() {
        return $this->hasMany(static::className(), ['position_id' => 'position_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getSiblingOrder() {
        return $this->getSibling()->orderBy([
            'listorder' => SORT_ASC,
            'updated_at' => SORT_DESC,
        ])->select('id')->column();
    }

    /**
     * {@inheritdoc}
     */
    public function getThumbUrl() {
        return Url::attach($this->thumb);
    }

    /**
     * {@inheritdoc}
     */
    public function getBadge() {
        return '推广';
    }

    /**
     * {@inheritdoc}
     */
    public function getWeblink() {
        if (!$this->map_id) {
            return null;
        }
        $data = $this->map_data;
        $mapData = null;
        if (!empty($data)) {
            $mapData = unserialize($data);
        }
        return Url::weblink($this->map_id, $mapData);
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id',
            'title',
            'description' => function() {
                return strval($this->description);
            },
            'badge',
            'thumb' => 'thumbUrl',
            'weblink',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public static function findRecent() {
        $query = static::find()->andWhere([
            'and',
            ['format' => 'image'],
            ['status' => static::STATUS_ACTIVE],
        ])->orderBy([
            'listorder' => SORT_ASC,
        ]);
        $data = $query->one();
        if ($data) {
            return $data;
        }
        return static::defaultSplash();
    }

}
